import { ref } from 'vue'
import { adapter, request } from '@/request'
import { phoenixCheckerByPost } from '@/core/api/priceSheet'
import type { RequestWrapper } from '@/core/interfaces/common'
import type { PhoenixSold } from '@/core/enums/platform'
import type { PhoenixPlatformEnum } from '@/core/enums/platform'

interface Params {
  keyword: string
  device: PhoenixPlatformEnum
}

export interface PhoenixCheckResult {
  device: PhoenixPlatformEnum
  keyword: string
  sold: PhoenixSold
}

export default function getPhoenixCheckResult() {
  const service = adapter<RequestWrapper<PhoenixCheckResult>>(request)
  const loading = ref(false)
  const response = ref<Partial<PhoenixCheckResult>>({})

  const fetch = (data: Params) => {
    const { url, method } = phoenixCheckerByPost()
    loading.value = true

    return service({
      config: {
        baseURL: import.meta.env.VITE_PHOENIX_PLUS_URL,
        url,
        method,
        data
      },
      callback: (res) => {
        response.value = res.data.data
      },
      error: (err) => {
        console.log(err)
      },
      done: () => {
        loading.value = false
      }
    })
  }

  return [response, fetch, loading] as const
}
